package com.yangff.service.impl;



import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangff.api.client.ProductClient;
import com.yangff.api.client.UserClient;
import com.yangff.entity.admin.entity.Productmanagement;
import com.yangff.entity.product.entity.Financialproduct;
import com.yangff.entity.product.entity.Fundnetvalue;
import com.yangff.entity.product.entity.dto.FinancialproductDTO;
import com.yangff.entity.product.entity.dto.FundnetvalueDTO;
import com.yangff.entity.product.entity.vo.ProductVo;
import com.yangff.entity.vo.ResultVo;
import com.yangff.entity.product.entity.dto.AutoStockDTO;
import com.yangff.mapper.ProductmanagementMapper;
import com.yangff.service.IProductmanagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 * 存储管理员对理财产品的管理操作记录 服务实现类
 * </p>
 *
 * @author yangff
 * @since 2025-01-15
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ProductmanagementServiceImpl extends ServiceImpl<ProductmanagementMapper, Productmanagement> implements IProductmanagementService {

    private final ProductClient productClient;
    private final UserClient userClient;

    private final RabbitTemplate rabbitTemplate;

    public ProductVo getProductByCode(String code)
    {
        return productClient.getProductByCode(code);
    }

    @Override
    public ProductVo saveProductByType(AutoStockDTO autoStockDTO) {
        return productClient.saveTypeProduct(autoStockDTO);
    }


    //注册基金
    @Override
    public ResultVo ListFund(String code, Double initialPrice) {
        //获取产品信息
        ProductVo productVo = getProductByCode(code);
        Productmanagement checkregister = getOne(new LambdaQueryWrapper<Productmanagement>().eq(Productmanagement::getProductCode, code)
                .eq(Productmanagement::getAction, "注册"));
        if (checkregister != null) return ResultVo.custom("该产品已注册，请勿重复注册");

        if (initialPrice == null || initialPrice <= 0) initialPrice = 1.0;
        //注册在管理表上

        Productmanagement productmanagement = new Productmanagement();
        productmanagement.setAction("注册");
        productmanagement.setDetails("管理员进行了注册");
        productmanagement.setProductCode(code);

        rabbitTemplate.convertAndSend("fts.direct", "saveproduct" , productmanagement);

        //注册在净值
        FundnetvalueDTO fundnetvalueDTO = new FundnetvalueDTO();
        fundnetvalueDTO.setCode(code);
        fundnetvalueDTO.setNetValue(initialPrice);


        rabbitTemplate.convertAndSend("fts.direct","list",fundnetvalueDTO);


        return ResultVo.success();
    }



    @Transactional(rollbackFor = Exception.class)
    public void saveProductlog(Productmanagement management) {
        Productmanagement productmanagement = new Productmanagement();
        productmanagement.setProductCode(management.getProductCode());
        productmanagement.setAction(management.getAction());
        productmanagement.setDetails(management.getDetails());
        save(productmanagement);
    }



    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public Boolean DelistFund(String code) {
        productClient.deleteFundNetValueByCode(code);
        userClient.deleteUserAssetByCode(code);

        return remove(new LambdaQueryWrapper<Productmanagement>().eq(Productmanagement::getProductCode, code));
    }

    //删除并退市
    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public void deleteProductByCode(String code) {
        productClient.deleteProductByCode(code);
        DelistFund(code);
    }



    @Override
    public Page<ProductVo> selectProduct(Pageable pageable) {
        return productClient.selectProduct(pageable);
    }


    @Override
    public ProductVo saveProduct(FinancialproductDTO financialproductDTO) {
        Financialproduct financialproduct = BeanUtil.copyProperties(financialproductDTO, Financialproduct.class);
        return productClient.saveProduct(financialproduct);
    }


}
